package com.cykj.net;

import java.util.HashMap;

public class HttpRequest {
    private String message;//请求的消息
    private String type;//请求类型
    private String module;//模块
    private String protocol;//协议
    private HashMap<String, String> parameterMap = new HashMap();
    private HashMap<String, String> headerMap = new HashMap();

    public HttpRequest(String message) {
        this.message = message;
        String[] split = message.split("\r\n");
        String requestLine = split[0];
        this.handleRequestLine(requestLine);
        this.handleParameter();
    }

    //处理请求行
    public void handleRequestLine(String requestLine) {
        String[] strings = requestLine.split(" ");
        this.setType(strings[0]);
        this.setProtocol(strings[2]);
        this.setModule(strings[1]);
    }
    public void handleParameter() {
        String[] split1;
        if (this.module.contains("?")) {
            System.out.println("请求行有参数");
            split1 = this.module.split("\\?");
            this.module = split1[0];
            this.lengthIsTwo(split1);
        }

        split1 = this.message.split("\r\n\r\n");
        this.lengthIsTwo(split1);
    }

    public void lengthIsTwo(String[] split) {
        if (split.length >= 2) {
            String[] parameters = split[1].split("&");
            String[] var3 = parameters;
            int var4 = parameters.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                String parameter = var3[var5];
                String[] kv = parameter.split("=");
                this.getParameterMap().put(kv[0], kv[1]);
            }
        }

    }

    public String getParameter(String key) {
        return (String)this.parameterMap.get(key);
    }

    public String toString() {
        return "HttpRequest{type='" + this.type + '\'' + ", module='" + this.module + '\'' + ", protocol='" + this.protocol + '\'' + ", parameterMap=" + this.parameterMap + ", headerMap=" + this.headerMap + '}';
    }

    public HashMap<String, String> getHeaderMap() {
        return this.headerMap;
    }

    public void setHeaderMap(HashMap<String, String> headerMap) {
        this.headerMap = headerMap;
    }

    public String getType() {
        return this.type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getModule() {
        return this.module;
    }

    public void setModule(String module) {
        this.module = module;
    }

    public String getProtocol() {
        return this.protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public HashMap<String, String> getParameterMap() {
        return this.parameterMap;
    }

    public void setParameterMap(HashMap<String, String> parameterMap) {
        this.parameterMap = parameterMap;
    }
}